﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;
using Common;

namespace BLL
{
    public abstract class BaseDAL<T> where T : BaseModel, new()
    {
        public abstract string tablename { get; }
        public abstract Dictionary<string, bool> PKName { get; }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">实体对象</param>
        /// <returns>bool类型 true为添加成功，false为添加失败</returns>
        public bool Add(T t)
        {

            string sql = Common.Ecommon.GetSql<T>(t, tablename, PKName);
            return DBHelp.GetExecuteNonQuery(sql);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns>List集合</returns>
        public List<T> Show(params SqlParameter[] paramters)
        {
            string sql = Common.Ecommon.GetSqlList<T>(tablename, paramters);
            SqlDataReader reader = DBHelp.GetReader(sql);
            List<T> list = new List<T>();
            while (reader.Read())
            {
                T t = new T();
                foreach (var item in t.GetType().GetProperties())
                {
                    //获取当前属性的名字
                    //item.Name
                    if (reader[item.Name] != DBNull.Value || reader[item.Name] != null)
                    {
                        if (item.PropertyType == typeof(DateTime))
                            item.SetValue(t, Convert.ToDateTime(reader[item.Name]), null);
                        else
                            item.SetValue(t, Convert.ChangeType(reader[item.Name], item.PropertyType), null);
                    }
                    else
                    {
                        item.SetValue(t, null, null);
                    }
                }
                list.Add(t);
            }
            return list;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Del(T t)
        {
            string sql = Common.Ecommon.DelSQL<T>(t, tablename, PKName);
            return DBHelp.GetExecuteNonQuery(sql);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Back(int id)
        {
            string sql = Common.Ecommon.BackSQL<T>(PKName, tablename, id);
            SqlDataReader reader = DBHelp.GetReader(sql);
            T t = new T();
            while (reader.Read())
            {


                foreach (var item in t.GetType().GetProperties())
                {
                    //获取当前属性的名字
                    //item.Name
                    if (reader[item.Name] != DBNull.Value || reader[item.Name] != null)
                    {
                        if (item.PropertyType == typeof(DateTime))
                            item.SetValue(t, Convert.ToDateTime(reader[item.Name]), null);
                        else
                            item.SetValue(t, Convert.ChangeType(reader[item.Name], item.PropertyType), null);
                    }
                    else
                    {
                        item.SetValue(t, null, null);
                    }
                }

            }
            return t;
        }

        /// <summary>
        /// 修改全部字段
        /// </summary>
        /// <param name="t"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Update(T t, int id)
        {
            string sql = Common.Ecommon.UpdSQL<T>(PKName, tablename, t, id);
            return DBHelp.GetExecuteNonQuery(sql);
        }


        /// <summary>
        /// 修改部分字段
        /// </summary>
        /// <param name="t"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Update1(int id ,params SqlParameter[] paramters)
        {
            string sql = Common.Ecommon.UptSQL<T>(PKName, tablename, id, paramters);
            return DBHelp.GetExecuteNonQuery(sql);
        }






    }
}
